<?php
$html = $_POST['html'];
$forwho = $_POST['forid'];
$answer = $_POST['answer'];
$name = $_POST['name'];
$userid = '';
function gennerateuserid($conn, $length = 7) {
    $characters = 'abcdefghijklmnopqrst1456789';
    $id = '';
    do {
        $id = '';
        for ($i = 0; $i < $length; $i++) {
            $id .= $characters[rand(0, strlen($characters) - 1)];
        }
        $result = $conn->query("SELECT 1 FROM exam_page WHERE exmatid = '$id'");
        if ($result !== false && $result->num_rows > 0) {
            $id = ''; 
        } else {
            break;
        }
    } while ($id === '');
    return $id;
}
require('../../../cofd/common.php');
$mysqli = $conn;
$userid = gennerateuserid($conn, 9);
$ansan_json = preg_replace('/\s+/', '', $ansan);
$time = date('Y-m-d H:i:s', time());
$number = 1;
$otherwb = '[0,0,0,[0,"0"]]';
$stmt = $mysqli->prepare("INSERT INTO exam_page (exmatid, exmatname, exmatfrom,exmatcreatetime,exmatzt,exmatother,exmattm,exmatanse) VALUES (?, ?, ?, ?, ?,?,?,?)");
$stmt->bind_param('ssssisss', $userid,$name, $forwho,$time,$number,$otherwb,$html,$answer); 
if($stmt->execute()){
    echo "<script>alert('创建试卷成功');location='../?mno=mr';</script>";  
}else{
    echo "<script>alert('创建试卷失败');location='../?mno=mr';</script>";  
}
$stmt->close();
$mysqli->close();